<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    :bodyStyle="bodyStyle"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top:15%;height: 70%;overflow-y: hidden"
  >
    <template slot="footer">
      <a-button key="back" v-if="isReadOnly" @click="handleCancel">
        关闭
      </a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" id="customerModal">
        <a-col :span="12">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="内部编码">
            <a-input disabled placeholder="请输入内部编码" v-decorator.trim="['contractCode', validatorRules.name]" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="合同号">
            <a-input disabled placeholder="请输入合同号" v-decorator.trim="['contractNum', validatorRules.name]" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="付款方式">
            <!-- <a-input placeholder="请选择付款方式" v-decorator.trim="['payment']" /> -->
            <a-select
              v-decorator.trim="['payment']"
              allowClear
              placeholder="请选择"
              showSearch
              optionFilterProp="children"
            >
              <a-select-option v-for="(item, index) in payMent" :key="index" :value="item.name">
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="已收金额">
            <!-- <a-input placeholder="请输入已收金额" v-decorator.trim="['received']" /> -->
            <a-input-number style="width:100%;" placeholder="请输入已收金额" v-decorator.trim="['received']" :min="1" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="付款日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-date-picker
              style="width: 100%"
              v-decorator.trim="['date']"
              format="YYYY-MM-DD"
              placeholder="请输入付款日期"
            />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="是否开票">
            <a-select
              v-decorator.trim="['isInvoiced']"
              allowClear
              placeholder="请选择"
              showSearch
              @change="handleChange"
              optionFilterProp="children"
            >
              <a-select-option v-for="(item, index) in isTrue" :key="index" :value="item.id">
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="12" v-if="isShow">
          <a-form-item label="开票日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-date-picker
              style="width: 100%"
              v-decorator.trim="['invoicedDate', isShow ? validatorRules.name : '']"
              format="YYYY-MM-DD"
              placeholder="请输入开票日期"
            />
          </a-form-item>
        </a-col>
        <a-col :span="12" v-if="isShow">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="开票金额">
            <!-- <a-input
              placeholder="请输入开票金额"
              v-decorator.trim="['invoicedAmount', isShow ? validatorRules.name : '']"
            /> -->
            <a-input-number
              style="width:100%;"
              placeholder="请输入开票金额"
              v-decorator.trim="['invoicedAmount', isShow ? validatorRules.name : '']"
              :min="1"
            />
          </a-form-item>
        </a-col>
      </a-form>
    </a-spin>
  </a-modal>
</template>
<script>
import pick from 'lodash.pick'
import { AddPayment, checkSupplier, EditPayment } from '@/api/api'
import { autoJumpNextInput } from '@/utils/util'
export default {
  name: 'AddEpayment',
  data() {
    return {
      title: '操作',
      mode: [{ name: '预计付款金额' }, { name: '预计付款百分比' }],
      bodyStyle: {
        padding: '20',
        height: window.innerHeight * 0.5 + 'px',
        'overflow-y': 'auto'
      },
      payMent: [
        {
          id: 0,
          name: '电承'
        },
        {
          id: 1,
          name: '电汇'
        }
      ],
      isTrue: [
        {
          id: 0,
          name: '未开票'
        },
        {
          id: 1,
          name: '已开票'
        },
        {
          id: 2,
          name: '不开票'
        }
      ],
      isShow: false,
      visible: false,
      model: {},
      record: null,
      isReadOnly: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        name: {
          rules: [
            { required: true, message: '请输入名称!' }
            // { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' },
            // { validator: this.validateSupplierName }
          ]
        }
      }
    }
  },
  created() {},
  methods: {
    handleChange(value) {
      if (value == 1) {
        this.isShow = true
      } else {
        this.isShow = false
      }
      console.log(value)
    },
    add(record) {
      this.record = record
      this.edit(record)
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      if (this.model.isInvoiced == 1) {
        this.isShow = true
      } else {
        this.isShow = false
      }
      console.log(this.model, 'sadasdasdsasda')
      this.$nextTick(() => {
        //编辑
        if (record.payment) {
          var invoicedDate = this.$moment(this.model.invoicedDate)
          this.form.setFieldsValue({ invoicedDate: invoicedDate })
          var date = this.$moment(this.model.date)
          this.form.setFieldsValue({ date: date })
        }
        this.form.setFieldsValue(
          pick(this.model, 'contractCode', 'contractNum', 'payment', 'received', 'invoicedAmount', 'isInvoiced')
        )
        autoJumpNextInput('customerModal')
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let formData = Object.assign(values)
          if (this.model.beginNeedGet && this.model.beginNeedPay) {
            that.$message.warn('期初应收和期初应付不能同时输入')
            that.confirmLoading = false
            return
          }
          if (!this.model.businessLeader) {
            formData.id = this.model.id
          }
          if (!this.isShow) {
            formData.invoicedAmount = 0
            formData.invoicedDate = ''
          } else {
            formData.invoicedDate = this.$moment(formData.invoicedDate).format('YYYY-MM-DD')
          }
          formData.contractCode = this.model.contractCode
          formData.contractNum = this.model.contractNum
          formData.date = this.$moment(formData.date).format('YYYY-MM-DD')

          let obj
          if (!this.model.businessLeader) {
            obj = EditPayment(formData)
          } else {
            obj = AddPayment(formData)
          }
          obj
            .then(res => {
              if (res.code === 200 || res.code === 10000) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.data)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    validateSupplierName(rule, value, callback) {
      let params = {
        name: value,
        type: '客户',
        id: this.model.id ? this.model.id : 0
      }
      checkSupplier(params).then(res => {
        if (res && res.code === 200) {
          if (!res.data.status) {
            callback()
          } else {
            callback('名称已经存在')
          }
        } else {
          callback(res.data)
        }
      })
    }
  }
}
</script>
<style scoped>
/** Button按钮间距 */
.table-operator .ant-btn {
  margin: 0 8px 8px 0;
}
@import '~@assets/less/common.less';
</style>
